/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package br.com.abelha.manager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import br.com.abelha.manager.db.DAOUser;
import br.com.abelha.manager.entity.User;
import br.com.abelha.manager.ws.RequestAbelhaService;


/**
 * @author dalsonsouza
 */ 

public final class AbelhaManager extends Activity
{

    public static final String TAG = "ContactManager";

    private Button loginButton;
    private Context context;
	private Button exitButton;
	SharedPreferences prefs;
    
    /**
     * Called when the activity is first created. Responsible for initializing the UI.
     */

	public void onCreate(Bundle savedInstanceState)
    {
        Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_manager);

        // Obtain handles to UI objects
        loginButton = (Button) findViewById(R.id.btLogin);
        exitButton = (Button) findViewById(R.id.sair);
        
        context = getApplicationContext();
        
        final DAOUser daoUser = new DAOUser(context);
        
        loginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
                Log.d(TAG, "mAddAccountButton clicked");
                
                AlertDialog.Builder dialog = new AlertDialog.Builder(AbelhaManager.this);
                dialog.setTitle("Atencao");
                dialog.setMessage("Usuario/Senha incorretos!");
                dialog.setCancelable(true);
                dialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
    				@Override
    				public void onClick(DialogInterface dialog, int which) {
    				}
    			});
                
                EditText usuario = (EditText)findViewById(R.id.usuario);
                EditText senha = (EditText)findViewById(R.id.senha);
                
                User user = daoUser.login(usuario.getText().toString(), senha.getText().toString());
                
                /**
                 * WS
                 */
                
                /*User user2 = new RequestAbelhaService().
                			user(usuario.getText().toString()).
                			pass(senha.getText().toString()).
                			uri("http://100.100.100.64/PFS/sincroniza").
                			requestColmeiaServer(); */
                
                
                //Construcao login via webservice
                
                Log.d(TAG, "User " + user);
                
                if (user != null) {
                	prefs = getSharedPreferences("Abelha", MODE_PRIVATE);
                	SharedPreferences.Editor editor = prefs.edit();
                	editor.putString("username", user.getNome_completo());
                	editor.commit();

                	launchLoginStartup();

                } else {
                	dialog.show();
                }
            }
        });
        
        exitButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
					finish();
			}
		});
        
    }

    /**
     * Launches the ContactAdder activity to add a new contact to the selected accont.
     */
    
    protected void launchLoginStartup() {
        Intent i = new Intent(this, LoginStartup.class);
        startActivity(i);
    }
}
